import {SendLoginWithRes, WithoutToken, WithFormUrlEncodedBody, CodeError, Net} from "foundation";
import {waitingRegistry} from "MVx";

class Response {
  public uid:string|null=null;
  public token:string|null=null;
}

export async function LoginByOAuth2(net:Net, jscode:string):Promise<CodeError|null>{
  let [_, err] = await SendLoginWithRes("/ts/wxappLogin/LoginByOAuth2",
    {jscode:jscode}, Response, net,(ret):{uid:string, token:string} => {
      return {uid:ret.uid!, token:ret.token!}
    },  WithFormUrlEncodedBody(), WithoutToken());

  let waiting = waitingRegistry.get(net.getBaseUrl())!;
  for (let w of waiting) {
    w(err)
  }

  waitingRegistry.set(net.getBaseUrl(), []);

  return err;
}
